package com.traveldream.entities;

import java.io.Serializable;

import javax.persistence.*;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the packet database table.
 * 
 */
@Entity
@NamedQueries({
	@NamedQuery(name="Packet.findAll", query="SELECT p FROM Packet p"),
	@NamedQuery(name="Packet.findByPlace",query="SELECT p FROM Packet p WHERE p.place.name LIKE :placeName"),
	@NamedQuery(name="Packet.findBeetwenDates",query="SELECT p FROM Packet p WHERE (p.startingDate > :start AND p.endingDate < :end)"),
	@NamedQuery(name="Packet.findByStartingDate",query="SELECT p FROM Packet p WHERE p.startingDate > :start"),
	@NamedQuery(name="Packet.findByEndingDate",query="SELECT p FROM Packet p WHERE p.endingDate < :end"),
	@NamedQuery(name="Packet.findBetweenDatesByPlace",query="SELECT p FROM Packet p WHERE  p.startingDate > :start AND p.endingDate < :end AND p.place.name LIKE :placeNamee")
})

public class Packet implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;

	@Temporal(TemporalType.DATE)
	@Column(name="creation_date")
	private Date creationDate;

	private String description;

	@Temporal(TemporalType.DATE)
	@Column(name="ending_date")
	private Date endingDate;

	private String name;

	@Temporal(TemporalType.DATE)
	@Column(name="starting_date")
	private Date startingDate;

	//bi-directional many-to-one association to Booking
	@OneToMany(mappedBy="packet")
	private List<Booking> bookings;

	//bi-directional one-to-one association to CustomPacket
	@OneToOne(mappedBy="packet")
	private CustomPacket customPacket;

	//bi-directional one-to-one association to DefaultPacket
	@OneToOne(mappedBy="packet")
	private DefaultPacket defaultPacket;

	//bi-directional many-to-one association to Place
	@ManyToOne
	@JoinColumn(name="PLACE_NAME")
	private Place place;

	//bi-directional many-to-many association to Product
	@ManyToMany
	@JoinTable(
			name="packet_products"
			, joinColumns={
					@JoinColumn(name="packet_id")
			}
			, inverseJoinColumns={
					@JoinColumn(name="product_id")
			}
	)
	private List<Product> products;

	public Packet() {
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getCreationDate() {
		return this.creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getEndingDate() {
		return this.endingDate;
	}

	public void setEndingDate(Date endingDate) {
		this.endingDate = endingDate;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getStartingDate() {
		return this.startingDate;
	}

	public void setStartingDate(Date startingDate) {
		this.startingDate = startingDate;
	}

	public List<Booking> getBookings() {
		return this.bookings;
	}

	public void setBookings(List<Booking> bookings) {
		this.bookings = bookings;
	}

	public Booking addBooking(Booking booking) {
		getBookings().add(booking);
		booking.setPacket(this);

		return booking;
	}

	public Booking removeBooking(Booking booking) {
		getBookings().remove(booking);
		booking.setPacket(null);

		return booking;
	}

	public CustomPacket getCustomPacket() {
		return this.customPacket;
	}

	public void setCustomPacket(CustomPacket customPacket) {
		this.customPacket = customPacket;
	}

	public DefaultPacket getDefaultPacket() {
		return this.defaultPacket;
	}

	public void setDefaultPacket(DefaultPacket defaultPacket) {
		this.defaultPacket = defaultPacket;
	}

	public Place getPlace() {
		return this.place;
	}

	public void setPlace(Place place) {
		this.place = place;
	}

	public List<Product> getProducts() {
		return this.products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}
	
	public float getPrice(){
		float price = 0;
		for(Product p:this.getProducts())
			price += p.getPrice();
		System.out.print("PacketPrice="+price);
		return price;
	}
	
	@Override
	public boolean equals(Object obj) {
	    if(this.id == ((Packet) obj).id) {
	        return true;
	    }else {
	        return false;
	    }
	}


}